import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			//数据
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
      search: "",
      template: {},
      templateList: [],
      ismodelShow: false
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		let that = this;
	},
	mounted() {},
	methods: { //写函数方法
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
			//此处调列表数据接口
      this.searchTodo();
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
    //显示修改对话框
    showModelShow: function(template) {
		  this.template = template;
		  $("#urlLi").html(this.getFileName(template.url));
		  this.ismodelShow = true;
    },
    closeModel: function() {
      this.ismodelShow = false;
    },
    //类型状态
    typeStatus: function(type) {
		  //0：其他；1:基地申请；2：项目申报；3：项目申请；4：合作合同；5：任务书；6：中期报告；7：结题报告
		  if (type != null) {
        switch (type) {
          case 0 :
            return "其他";
          case 1 :
            return "基地申请";
          case 2 :
            /*return "项目申报";*/
            return "项目需求说明书";
          case 3 :
            /*return "项目申请";*/
            return "项目申请书";
          case 4 :
            return "合作合同";
          case 5 :
            return "任务书";
          case 6 :
            return "中期报告";
          case 7 :
            return "结题报告";
        }
		  }
    },
    //搜索数据列表
    searchTodo: function() {
      let that = this;
      that.$post(
        that.urls.getTemplatePageInfo, {
          "pageNum": that.page,
          "pageSize": that.size,
          "name": that.search
        }
      )
      .then(function(res) {
        if(res.success) {
          that.templateList = res.pageInfo.list;
          that.total = res.pageInfo.total;
          that.page = res.pageInfo.pageNum;
        }
      })
    },
    //删除数据
    deleteData: function(id) {
		  if (id != null) {
        let that = this;
        that.$post(
          that.urls.deleteTemplate,
          { "id": id }
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "删除成功！";
            that.searchTodo();
          } else {
            msg = "删除失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
    //跳转修改页面
    gotoUpdate: function(uuid) {
      this.remove("templateUuid");
		  if (uuid != null && uuid != "") {
        this.set("templateUuid", uuid);
        this.$router.push({
          path: "/AddTemplate",
        })
      }
    },
    //跳转到添加页面
    toAdd: function() {
      this.remove("templateUuid");
      this.$router.push({
        path: "/AddTemplate",
      })
    },
		// 下载模板
    download: function(url) {
			if (url != null && url != "") {
        window.location.href = this.urls.download + "?path=" + url;
      }
		},
    touchOffIdInput(n) {
      this.uploadNum = n;
      if(n == 1) {
        $("#handIdFront").click();
      }
    },
    //删除附件
    deleteEnclosure: function() {
      $("#urlLi").html("");
      $("input[name='url']").val("");
    },
    idFrontImg: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfo = res.data[0];
            $("input[name='url']").val(fileInfo.url);
            $("#urlLi").html(that.getFileName(fileInfo.url));
          }
        })
    },
    //修改
    updateData: function() {
      let that = this;
      let url = $("input[name='url']").val();
      let id = that.template.id;
      if (url == null && url == "") {
        layer.msg("请选择文件！", {
          time: 3000, //3s后自动关闭
        });
        return;
      }
      if(id != null) {
        that.$post(
          that.urls.updateTemplate,
          {
            "id" : id,
            "url" : url
          }
        )
        .then(function(res) {
          let msg = "";
          if(res.success) {
            msg = "修改成功！";
            that.searchTodo();
            that.ismodelShow = false;
          } else {
            msg = "修改失败！";
          }
          layer.msg(msg, {
            time: 3000, //3s后自动关闭
          });
        })
      }
    },
	}
}
